package com.starsea.book.ui.newpage

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.fragment.app.activityViewModels
import com.blankj.utilcode.util.BarUtils
import com.starsea.book.R
import com.starsea.book.base.VMBaseFragment
import com.starsea.book.databinding.FragmentMineBinding
import com.starsea.book.ui.config.ConfigActivity
import com.starsea.book.ui.config.ConfigTag
import com.starsea.book.ui.newpage.activity.FeedbackActivity
import com.starsea.book.ui.newpage.activity.PersonalPageActivity
import com.starsea.book.ui.newpage.activity.SetActivity
import com.starsea.book.ui.newpage.viewmodel.UserViewModel
import com.starsea.book.ui.newpage.activity.RealNameActivity
import com.starsea.book.ui.newpage.utils.finialDouble
import com.starsea.book.ui.newpage.utils.loadRoundImage
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2023-12-26 09 GMT+8
 * @desc :
 */
class MineFragment() : VMBaseFragment<UserViewModel>(R.layout.fragment_mine) {

    private val binding by viewBinding(FragmentMineBinding::bind)
    override val viewModel by activityViewModels<UserViewModel>()

    constructor(position: Int) : this() {
        val bundle = Bundle()
        bundle.putInt("position", position)
        arguments = bundle
    }

    override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {

        BarUtils.addMarginTopEqualStatusBarHeight(binding.cvHeader)
        binding.cvHeader.setOnClickListener {
            startActivity<ConfigActivity>("configTag" to ConfigTag.USER_PROFILE_CONFIG)
        }
        binding.rtvSetting.setOnClickListener {
            startActivity<ConfigActivity>("configTag" to ConfigTag.SETTING_CONFIG)
        }
        binding.rtvFeedBack.setOnClickListener {
            startActivity<FeedbackActivity>()
        }

        binding.rtvMsg.setOnClickListener {
            startActivity<MsgActivity>()
        }

        binding.rtvReadHistory.setOnClickListener {
            startActivity<ReadHistoryActivity>()
        }

        binding.rtvOpenVip.setOnClickListener {
            startActivity<VipActivity>()
        }

        binding.llVip.setOnClickListener {
            startActivity<VipActivity>()
        }

        viewModel.userProfileResult.observe(this) {
            if (it.status == 200) {
                binding.ivHeader.loadRoundImage(requireContext(), it.data!!.headUrl)
                binding.tvNickName.text = it.data!!.custName
                binding.rtvTag.text = "LV${it.data!!.levelId}"
                if (it.data!!.isVip == 1) {
                    binding.llVip.visibility = View.GONE
                } else {
                    binding.llVip.visibility = View.VISIBLE
                }

                if (TextUtils.isEmpty(it.data!!.goldBalance)) {
                    binding.tvTotalGoldCoin.text = "0"
                } else {
                    val parseDouble = it.data!!.goldBalance!!.toDouble()

                    if (parseDouble > 10000) {
                        binding.tvTotalGoldCoin.text =
                            (parseDouble / 10000).finialDouble().plus("万")
                    } else {
                        binding.tvTotalGoldCoin.text = (parseDouble).finialDouble().plus("万")
                    }

                }

                if (TextUtils.isEmpty(it.data!!.todayGold)) {
                    binding.tvTodayGoldCoin.text = "0"
                } else {
                    val parseDouble = it.data!!.todayGold!!.toDouble()

                    if (parseDouble > 10000) {
                        binding.tvTodayGoldCoin.text =
                            (parseDouble / 10000).finialDouble().plus("万")
                    } else {
                        binding.tvTodayGoldCoin.text = (parseDouble).finialDouble().plus("万")
                    }

                }

                binding.tvTodayReadTime.text = "${it.data!!.todayReadTimes ?: "0"}"


            } else {
                binding.tvNickName.text = "未登陆"
            }
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.getUserProfile()
    }
}